<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/6/17 下午3:26
 * +-------------------------------------------------------------
 */

namespace app\common\traits;

use think\response\Json;

trait JsonTrait
{

    /**
     * 返回操作成功json
     * @param array|string $data
     * @param string $message
     * @return Json
     */
    protected function success($data = [], string $message = 'success'): Json
    {
        if (is_string($data)) {
            $message = $data;
            $data    = [];
        }
        return json(['code' => config('status.success'), 'msg' => $message, 'data' => $data]);
    }

    /**
     * 返回操作失败json
     * @param string $message
     * @param array $data
     * @return Json
     */
    protected function error(string $message = 'error', array $data = []): Json
    {
        return json(['code' => config('status.error'), 'msg' => $message, 'data' => $data]);
    }

    /**
     * 返回未登录json
     * @param string $message
     * @param array $data
     * @return Json
     */
    protected function errLogin(string $message = '未登录', array $data = []): Json
    {
        return json(['code' => config('status.not_logged'), 'msg' => $message, 'data' => $data]);
    }

    /**
     * 返回无权限json
     * @param string $message
     * @param array $data
     * @return Json
     */
    protected function errPermission(string $message = '无权限访问', array $data = []): Json
    {
        return json(['code' => config('status.not_permission'), 'msg' => $message, 'data' => $data]);
    }

}